package com.yuexunit.operationman.nbsy.module.Login.ui;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;


import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.Login.bean.UpdateBean;
import com.yuexunit.operationman.nbsy.module.Login.bean.UserBean;
import com.yuexunit.operationman.nbsy.module.Login.model.impl.LoginModel;
import com.yuexunit.operationman.nbsy.module.Login.model.impl.UpdateModel;
import com.yuexunit.operationman.nbsy.module.Login.presenter.ILoginPresenter;
import com.yuexunit.operationman.nbsy.module.Login.presenter.IUpdatePresenter;
import com.yuexunit.operationman.nbsy.module.Login.presenter.impl.LoginPresenter;
import com.yuexunit.operationman.nbsy.module.Login.presenter.impl.UpdatePresenter;
import com.yuexunit.operationman.nbsy.module.Login.ui.view.ILoginView;
import com.yuexunit.operationman.nbsy.module.Login.ui.view.IUpdateView;
import com.yuexunit.operationman.nbsy.util.UpdateManager;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2017/2/24.
 */
public class Act_Login extends BaseKJActivity implements ILoginView,IUpdateView{

    @Bind(R.id.username_edit)
    EditText usernameEdit;
    @Bind(R.id.password_edit)
    EditText passwordEdit;
    @Bind(R.id.vewsionname)
    TextView vewsionname;

    LoadingDialog loadingDialog;

    ILoginPresenter presenter;
    IUpdatePresenter updatePresenter;


    @Override
    public void setRootView() {
        setContentView(R.layout.act_login);
        ButterKnife.bind(this);
    }


    @Override
    public void initData() {
        super.initData();
        if((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0){
            finish();
            return;
        }
        loadingDialog = new LoadingDialog(this);
        presenter = new LoginPresenter(this,new LoginModel());
        updatePresenter = new UpdatePresenter(new UpdateModel(),this);
        usernameEdit.setText("9188");
        passwordEdit.setText("@5356DNS");
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        updatePresenter.update();
    }

    @OnClick(R.id.login)
    public void onClick() {
        final String user_Id = usernameEdit.getText().toString().trim();
        if(TextUtils.isEmpty(user_Id)) {
            ViewInject.toast("请输入工号");
            return;
        }

        final String password = passwordEdit.getText().toString().trim();
        if(TextUtils.isEmpty(password)) {
            ViewInject.toast("请输入密码");
            return;
        }

        Map<String, String> params = new HashMap<>();
        params.put("jobNum", usernameEdit.getText().toString());
        params.put("loginPwd", passwordEdit.getText().toString());
        presenter.getUserDate(params);
    }

    @Override
    public void getUserDate(UserBean userBean) {
        if(userBean.getCode() == 200)
        {
            PreferenceHelper.write(aty, AppConfig.USER_INFO, AppConfig.USER_ACCOUNT, userBean.getObject().get(0).getAccountId());
            PreferenceHelper.write(aty, AppConfig.USER_INFO, AppConfig.USER_FULLNAME, userBean.getObject().get(0).getFullName());
            PreferenceHelper.write(aty, AppConfig.USER_INFO, AppConfig.DOCTORTYPE, userBean.getObject().get(0).getDoctorType());

            if(userBean.getObject().get(0).getDoctorType().equals("H")) {
                startActivity(new Intent(aty, Act_Search.class));
            }
            else if(userBean.getObject().get(0).getDoctorType().equals("Y")||
                    userBean.getObject().get(0).getDoctorType().equals("M"))
            {
                startActivity(new Intent(aty, Act_PatientList.class));
            }
        }
        else {
            ViewInject.toast(userBean.getDesc());
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }

    @Override
    public void showUpdateInfo(UpdateBean updateBean) {
        try {
            int versionCode = getPackageManager().getPackageInfo(getPackageName(),0).versionCode;
            if(updateBean.getVersionCode() > versionCode) {
                UpdateManager updateManager = new UpdateManager(this, updateBean, versionCode);
                updateManager.showNoticeDialog();
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
}
